package com.jy.rhin.application.cda;

import com.jy.api.hospital.HospitalAPI;
import com.jy.api.hospital.dto.HospitalDTO;
import com.jy.api.misc.dto.DaiMaDto;
import com.jy.api.misc.dto.ZhiYuMXCXDto;
import com.jy.api.patient.dto.BingRenXXDto;
import com.jy.common.StringUtil;
import com.jy.framework.base.utils.Converter;
import com.jy.rhin.application.cda.enums.HealthDictionaryEnum;
import com.jy.rhin.domain.cda.CDACreator;
import com.jy.rhin.infra.cda.CDAException;
import com.jy.rhin.infra.cda.feign.PatientService;
import com.jy.rhin.infra.cda.feign.ShuYuService;
import com.jy.rhin.infra.cda.service.ZhongXingJSBSFService;
import com.jy.rhin.infra.cda.util.ZhiJiUtils;
import com.jy.rhin.infra.cda.zxjsb.JingShenBSFVo;
import com.jy.rhin.infra.cda.zxjsb.JingShenBSFYYVo;
import com.jy.rhin.model.ClinicalDocumentData;
import com.jy.rhin.model.CodeObject;
import com.jy.rhin.model.ValueUnit;
import com.jy.rhin.model.ZhongXingJSBSF;
import com.jy.rhin.model.body.gaoxueysf.XiaCiSFAP;
import com.jy.rhin.model.body.gaoxueysf.ZhuanZhenJY;
import com.jy.rhin.model.body.zhongxingjsbsf.*;
import com.jy.rhin.model.header.*;
import com.jy.rhin.utils.CDAUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ZhongXingJSBSFApplication {

    @Autowired
    private CDACreator cdaCreator;
    @Autowired
    private ZhongXingJSBSFService zhongXingJSBSFService;
    @Autowired
    private PatientService patientService;
   /* @Autowired
    private DataMigrationAPI dataMigrationAPI;*/
    @Autowired
    private ShuYuService shuYuService;
    @Resource
    private HospitalAPI hospitalAPI;

    public String create(String jingShenBSFJLID, String yeWuJGDM) {
        return cdaCreator.create(createZhongXingJSBSF(jingShenBSFJLID, yeWuJGDM), "15zhongxingjsbsf.ftl");
    }

    public ClinicalDocumentData createZhongXingJSBSF(String jingShenBSFJLID, String yeWuJGDM) {
        JingShenBSFVo jingShenBSFVo = zhongXingJSBSFService.get(jingShenBSFJLID, yeWuJGDM);
        if (Objects.isNull(jingShenBSFVo)) {
            throw new CDAException("主业务数据不存在", null);
        }
        BingRenXXDto bingRenInfo = patientService.queryPatientById(jingShenBSFVo.getPATIENTID());
        if (Objects.isNull(bingRenInfo)) {
            throw new CDAException("患者信息不存在", null);
        }
        HospitalDTO hospital = hospitalAPI.info(yeWuJGDM).getData();
//        HospitalDTO hospital = dataMigrationAPI.getByHospitalId(yeWuJGDM).getData();
        if (Objects.isNull(hospital)) {
            throw new CDAException("机构数据不存在", null);
        }

        List<JingShenBSFYYVo> jingShenBSFYYs = zhongXingJSBSFService.getYongYao(jingShenBSFJLID, yeWuJGDM);

        shuYuZhiYuQuery(jingShenBSFVo, jingShenBSFYYs);

        ZhongXingJSBSF docData = new ZhongXingJSBSF();
        docData.setId(jingShenBSFJLID);
        setClinicalDocumentData(docData, jingShenBSFVo, hospital, bingRenInfo);

        SuiFangSJ suiFangSJ = new SuiFangSJ();
        suiFangSJ.setHuanZheFDBZ(jingShenBSFVo.getZHONGXINGJSJBHZFDBZ() == 1 ? "true" : "false");
        docData.setSuiFangSJ(suiFangSJ);

        ZhuYaoJKWT zhuYaoJKWT = new ZhuYaoJKWT();
        zhuYaoJKWT.setWeiXianXFJ(new CodeObject(jingShenBSFVo.getWEIXIANXINGFJDM(), jingShenBSFVo.getWEIXIANXINGFJMC()));
        zhuYaoJKWT.setJingShenZZ(new CodeObject(jingShenBSFVo.getJINGSHENZZDM(), jingShenBSFVo.getJINGSHENZZMC()));
        zhuYaoJKWT.setZiZhiLPJJG(new CodeObject(Converter.toString(jingShenBSFVo.getZIZHILIPJJGDM()), jingShenBSFVo.getZIZHILIPJGGMC()));
        zhuYaoJKWT.setShuiMiaoQK(new CodeObject(Converter.toString(jingShenBSFVo.getSHUIMIANQKDM()), jingShenBSFVo.getSHUIMIANQKMC()));
        zhuYaoJKWT.setYinShiHLXPJLB(new CodeObject(jingShenBSFVo.getYINSHIQKDM(), jingShenBSFVo.getYINSHIQKMC()));
        if (jingShenBSFVo.getGERENSHLLDM() != null) {
            zhuYaoJKWT.setSheHuiGNQKFL(new CodeObject("1", "个人生活料理"));
            zhuYaoJKWT.setSheHuiGNQKPJ(new CodeObject(Converter.toString(jingShenBSFVo.getGERENSHLLDM()), jingShenBSFVo.getGERENSHLLMC()));
        } else if (jingShenBSFVo.getJIAWULDDM() != null) {
            zhuYaoJKWT.setSheHuiGNQKFL(new CodeObject("2", "家务劳动"));
            zhuYaoJKWT.setSheHuiGNQKPJ(new CodeObject(Converter.toString(jingShenBSFVo.getJIAWULDDM()), jingShenBSFVo.getJIAWULDMC()));
        } else if (!StringUtil.isNullOrWhiteSpace(jingShenBSFVo.getSHENGCHANGLDJGZDM())) {
            zhuYaoJKWT.setSheHuiGNQKFL(new CodeObject("3", "生产劳动及工作"));
            zhuYaoJKWT.setSheHuiGNQKPJ(new CodeObject(jingShenBSFVo.getSHENGCHANGLDJGZDM(), jingShenBSFVo.getSHENGCHANGLDJGZMC()));
        } else if (!StringUtil.isNullOrWhiteSpace(jingShenBSFVo.getXUEXINLDM())) {
            zhuYaoJKWT.setSheHuiGNQKFL(new CodeObject("4", "学习能力"));
            zhuYaoJKWT.setSheHuiGNQKPJ(new CodeObject(jingShenBSFVo.getXUEXINLDM(), jingShenBSFVo.getXUEXINLMC()));
        } else if (!StringUtil.isNullOrWhiteSpace(jingShenBSFVo.getSHEHUIRJJWDM())) {
            zhuYaoJKWT.setSheHuiGNQKFL(new CodeObject("5", "社会人际交往"));
            zhuYaoJKWT.setSheHuiGNQKPJ(new CodeObject(jingShenBSFVo.getSHEHUIRJJWDM(), jingShenBSFVo.getSHEHUIRJJWMC()));
        }
        if (jingShenBSFVo.getQINGDUZSCS() > 0) {
            zhuYaoJKWT.setHuanBingSHYXLB(new CodeObject("2", "轻度滋事"));
            zhuYaoJKWT.setHuanBingSHYXCS(Converter.toString(jingShenBSFVo.getQINGDUZSCS()));
        } else if (jingShenBSFVo.getZHAOSHICS() > 0) {
            zhuYaoJKWT.setHuanBingSHYXLB(new CodeObject("3", "肇事"));
            zhuYaoJKWT.setHuanBingSHYXCS(Converter.toString(jingShenBSFVo.getZHAOSHICS()));
        } else if (jingShenBSFVo.getZHAOHUOCS() > 0) {
            zhuYaoJKWT.setHuanBingSHYXLB(new CodeObject("4", "肇祸"));
            zhuYaoJKWT.setHuanBingSHYXCS(Converter.toString(jingShenBSFVo.getZHAOHUOCS()));
        } else if (jingShenBSFVo.getZISHANGCS() > 0) {
            zhuYaoJKWT.setHuanBingSHYXLB(new CodeObject("5", "自伤"));
            zhuYaoJKWT.setHuanBingSHYXCS(Converter.toString(jingShenBSFVo.getZISHANGCS()));
        } else if (jingShenBSFVo.getZISHAWSCS() > 0) {
            zhuYaoJKWT.setHuanBingSHYXLB(new CodeObject("6", "自杀未遂"));
            zhuYaoJKWT.setHuanBingSHYXCS(Converter.toString(jingShenBSFVo.getZISHAWSCS()));
        } else if (jingShenBSFVo.getQITAWHXWCS() > 0) {
            zhuYaoJKWT.setHuanBingSHYXLB(new CodeObject("9", "其他"));
            zhuYaoJKWT.setHuanBingSHYXCS(Converter.toString(jingShenBSFVo.getQITAWHXWCS()));
        } else {
            zhuYaoJKWT.setHuanBingSHYXLB(new CodeObject("1", "无"));
            zhuYaoJKWT.setHuanBingSHYXCS("0");
        }
        zhuYaoJKWT.setGuanSuoQK(new CodeObject(Converter.toString(jingShenBSFVo.getGUANSUOQKDM()), jingShenBSFVo.getGUANSUOQKMC()));
        docData.setZhuYaoJKWT(zhuYaoJKWT);

        ZhuYuanShi zhuYuanShi = new ZhuYuanShi();
        zhuYuanShi.setMoCiZYSJ(CDAUtil.toD8(jingShenBSFVo.getCHUYUANRQ()));
        zhuYuanShi.setZhuYuanQK(new CodeObject(Converter.toString(jingShenBSFVo.getZHUYUANQKDM()), jingShenBSFVo.getZHUYUANQKMC()));
        docData.setZhuYuanShi(zhuYuanShi);

        ShiYanSJC shiYanSJC = new ShiYanSJC();
        shiYanSJC.setFuZhuJCBZ(jingShenBSFVo.getFUZHUJCBZ() == 1 ? "true" : "false");
        shiYanSJC.setJianCeRQ(CDAUtil.toD8(jingShenBSFVo.getJIANCHARQ()));
        shiYanSJC.setFuZhuJCXM(!StringUtil.isNullOrEmpty(jingShenBSFVo.getFUZHUJCXM()) ? jingShenBSFVo.getFUZHUJCXM() : "-");
        shiYanSJC.setFuZhuJCRYXM(jingShenBSFVo.getJIANCHARYMC());
        shiYanSJC.setFuZhuJCJG(!StringUtil.isNullOrEmpty(jingShenBSFVo.getFUZHUJCJG()) ? jingShenBSFVo.getFUZHUJCJG() : "-");
        docData.setShiYanSJC(shiYanSJC);

        YongYao yongYao = new YongYao();
        if (!CollectionUtils.isEmpty(jingShenBSFYYs)) {
            for (JingShenBSFYYVo infoVo : jingShenBSFYYs) {
                yongYao.setZhongYaoSYLB(new CodeObject(infoVo.getZHONGYAOSYLBBM(), infoVo.getZHONGYAOSYLBMC()));
                YongYaoItem yongYaoItem = new YongYaoItem();
                yongYaoItem.setYongYaoTJ(new CodeObject(Converter.toString(infoVo.getYONGYAOTJDM()), infoVo.getYONGYAOTJMC()));
                yongYaoItem.setDanCiYYJL(new ValueUnit(Converter.toString(new BigDecimal(infoVo.getYAOWUSYCJL()).setScale(2, BigDecimal.ROUND_HALF_UP)), infoVo.getYAOWUSYCJLDW()));
                yongYaoItem.setYaoWuSYPL(new ValueUnit(Converter.toString(infoVo.getYAOWUMRSYCS()), "次/日"));
                yongYaoItem.setYaoPinMC(infoVo.getYAOWUMC());
                yongYaoItem.setFuYaoYCX(new CodeObject(jingShenBSFVo.getFUYAOYCXDM(), jingShenBSFVo.getFUYAOYCXMC().replace("规律服药", "规律")));
                yongYaoItem.setYongYaoFZY(jingShenBSFVo.getYAOWUBLFYMS());
                yongYaoItem.setYongYaoFZYBZ("1".equals(jingShenBSFVo.getYAOWUBLFYBZ()) ? "true" : "false");
                yongYaoItem.setYaoWuSYZJL(new ValueUnit(Converter.toString(new BigDecimal(infoVo.getYAOWUSYCJL())
                        .multiply(new BigDecimal(infoVo.getYAOWUMRSYCS())).multiply(new BigDecimal(infoVo.getYONGYAOTS())).setScale(2, BigDecimal.ROUND_HALF_UP)), infoVo.getYAOWUSYCJLDW()));
                yongYao.getYongYaoItemList().add(yongYaoItem);
            }
        }
        docData.setYongYao(yongYao);

        JianKangPG jianKangPG = new JianKangPG();
        jianKangPG.setSuiFangPJ(new CodeObject(jingShenBSFVo.getCICISFFL(), jingShenBSFVo.getCICISFJGPJMC()));
        docData.setJianKangPG(jianKangPG);

        JianKangZD jianKangZD = new JianKangZD();
        jianKangZD.setKangFuCSZD(!StringUtil.isNullOrEmpty(jingShenBSFVo.getJINGSHENJKFCSMC()) ? jingShenBSFVo.getJINGSHENJKFCSMC() : "-");
        if (jingShenBSFVo.getJINGSHENJKFCSDM() != null) {
            jianKangZD.getJingShenKFCSs().add(new CodeObject(Converter.toString(jingShenBSFVo.getJINGSHENJKFCSDM()), jingShenBSFVo.getJINGSHENJKFCSMC()));
        }
        docData.setJianKangZD(jianKangZD);

        ZhuanZhenJY zhuanZhenJY = new ZhuanZhenJY();
        zhuanZhenJY.setZhuanZhenBZ(jingShenBSFVo.getSHIFOUZZ() == 1 ? "true" : "false");
        zhuanZhenJY.setZhuanZhenYY(!StringUtil.isNullOrEmpty(jingShenBSFVo.getZHUANZHENYYMS()) ? jingShenBSFVo.getZHUANZHENYYMS() : "-");
        zhuanZhenJY.setZhuanRuJGMC(!StringUtil.isNullOrEmpty(jingShenBSFVo.getZHUANZHENYYMC()) ? jingShenBSFVo.getZHUANZHENYYMC() : "-");
        zhuanZhenJY.setZhuanRuKSMC(!StringUtil.isNullOrEmpty(jingShenBSFVo.getZHUANZHENYYKSMC()) ? jingShenBSFVo.getZHUANZHENYYKSMC() : "-");
        docData.setZhuanZhenJY(zhuanZhenJY);

        XiaCiSFAP xiaCiSFAP = new XiaCiSFAP();
        xiaCiSFAP.setXiaCiSFRQ(CDAUtil.toD8(jingShenBSFVo.getXIACISFRQ()));
        docData.setXiaCiSFAP(xiaCiSFAP);

        return docData;
    }

    private void shuYuZhiYuQuery(JingShenBSFVo jingShenBSFVo, List<JingShenBSFYYVo> jingShenBSFYYs) {
        List<ZhiYuMXCXDto> zhiJiBSFList = new ArrayList<>();
        if (StringUtil.isNullOrEmpty(jingShenBSFVo.getWEIXIANXINGFJMC())) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV05_10_013.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(jingShenBSFVo.getWEIXIANXINGFJDM()))));
        }
        if (StringUtil.isNullOrEmpty(jingShenBSFVo.getJINGSHENZZMC())) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV04_01_009.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(jingShenBSFVo.getJINGSHENZZDM()))));
        }
        if (StringUtil.isNullOrEmpty(jingShenBSFVo.getZIZHILIPJGGMC())) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_ZZLPJ.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(jingShenBSFVo.getZIZHILIPJJGDM()))));
        }
        if (StringUtil.isNullOrEmpty(jingShenBSFVo.getSHUIMIANQKMC())) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_SMQK.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(jingShenBSFVo.getSHUIMIANQKDM()))));
        }
        if (StringUtil.isNullOrEmpty(jingShenBSFVo.getYINSHIQKMC())) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_YSQK.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(jingShenBSFVo.getYINSHIQKDM()))));
        }
        if (jingShenBSFVo.getGERENSHLLDM() != null) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_SHGNQK.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(jingShenBSFVo.getGERENSHLLDM()))));
        } else if (jingShenBSFVo.getJIAWULDDM() != null) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_SHGNQK.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(jingShenBSFVo.getJIAWULDDM()))));
        } else if (!StringUtil.isNullOrWhiteSpace(jingShenBSFVo.getSHENGCHANGLDJGZDM())) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_SHGNQK.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(jingShenBSFVo.getSHENGCHANGLDJGZDM()))));
        } else if (!StringUtil.isNullOrWhiteSpace(jingShenBSFVo.getXUEXINLDM())) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_SHGNQK.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(jingShenBSFVo.getXUEXINLDM()))));
        } else if (!StringUtil.isNullOrWhiteSpace(jingShenBSFVo.getSHEHUIRJJWDM())) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_SHGNQK.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(jingShenBSFVo.getSHEHUIRJJWDM()))));
        }
        if (StringUtil.isNullOrEmpty(jingShenBSFVo.getGUANSUOQKMC())) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_GSQKDMB.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(jingShenBSFVo.getGUANSUOQKDM()))));
        }
        if (StringUtil.isNullOrEmpty(jingShenBSFVo.getZHUYUANQKMC())) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_ZYQK.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(jingShenBSFVo.getZHUYUANQKDM()))));
        }
        if (StringUtil.isNullOrEmpty(jingShenBSFVo.getFUYAOYCXMC())) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_FYYCX.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(jingShenBSFVo.getFUYAOYCXDM()))));
        }
        if (jingShenBSFVo.getJINGSHENJKFCSDM() != null) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV06_00_209.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(jingShenBSFVo.getJINGSHENJKFCSDM()))));
        }
        if (!CollectionUtils.isEmpty(jingShenBSFYYs)) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV06_00_102.getCode(), jingShenBSFYYs.stream().map(o -> Converter.toString(o.getYONGYAOTJDM())).collect(Collectors.toSet()));
        }

        Map<String, List<DaiMaDto>> valueByCode = shuYuService.getValueByCode(zhiJiBSFList.toArray(new ZhiYuMXCXDto[0]));
        if (StringUtil.isNullOrEmpty(jingShenBSFVo.getWEIXIANXINGFJMC())) {
            jingShenBSFVo.setWEIXIANXINGFJMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV05_10_013.getCode(), Converter.toString(jingShenBSFVo.getWEIXIANXINGFJDM()), valueByCode));
        }
        if (StringUtil.isNullOrEmpty(jingShenBSFVo.getJINGSHENZZMC())) {
            jingShenBSFVo.setJINGSHENZZMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV04_01_009.getCode(), Converter.toString(jingShenBSFVo.getJINGSHENZZDM()), valueByCode));
        }
        if (StringUtil.isNullOrEmpty(jingShenBSFVo.getZIZHILIPJGGMC())) {
            jingShenBSFVo.setZIZHILIPJGGMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_ZZLPJ.getCode(), Converter.toString(jingShenBSFVo.getZIZHILIPJJGDM()), valueByCode));
        }
        if (StringUtil.isNullOrEmpty(jingShenBSFVo.getSHUIMIANQKMC())) {
            jingShenBSFVo.setSHUIMIANQKMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_SMQK.getCode(), Converter.toString(jingShenBSFVo.getSHUIMIANQKDM()), valueByCode));
        }
        if (StringUtil.isNullOrEmpty(jingShenBSFVo.getYINSHIQKMC())) {
            jingShenBSFVo.setYINSHIQKMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_YSQK.getCode(), Converter.toString(jingShenBSFVo.getYINSHIQKDM()), valueByCode));
        }
        if (jingShenBSFVo.getGERENSHLLDM() != null) {
            jingShenBSFVo.setGERENSHLLMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_SHGNQK.getCode(), Converter.toString(jingShenBSFVo.getGERENSHLLDM()), valueByCode));
        } else if (jingShenBSFVo.getJIAWULDDM() != null) {
            jingShenBSFVo.setJIAWULDMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_SHGNQK.getCode(), Converter.toString(jingShenBSFVo.getJIAWULDDM()), valueByCode));
        } else if (!StringUtil.isNullOrWhiteSpace(jingShenBSFVo.getSHENGCHANGLDJGZDM())) {
            jingShenBSFVo.setSHENGCHANGLDJGZMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_SHGNQK.getCode(), Converter.toString(jingShenBSFVo.getSHENGCHANGLDJGZDM()), valueByCode));
        } else if (!StringUtil.isNullOrWhiteSpace(jingShenBSFVo.getXUEXINLDM())) {
            jingShenBSFVo.setXUEXINLMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_SHGNQK.getCode(), Converter.toString(jingShenBSFVo.getXUEXINLDM()), valueByCode));
        } else if (!StringUtil.isNullOrWhiteSpace(jingShenBSFVo.getSHEHUIRJJWDM())) {
            jingShenBSFVo.setSHEHUIRJJWMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_SHGNQK.getCode(), Converter.toString(jingShenBSFVo.getSHEHUIRJJWDM()), valueByCode));
        }
        if (StringUtil.isNullOrEmpty(jingShenBSFVo.getGUANSUOQKMC())) {
            jingShenBSFVo.setGUANSUOQKMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_GSQKDMB.getCode(), Converter.toString(jingShenBSFVo.getGUANSUOQKDM()), valueByCode));
        }
        if (StringUtil.isNullOrEmpty(jingShenBSFVo.getZHUYUANQKMC())) {
            jingShenBSFVo.setZHUYUANQKMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_ZYQK.getCode(), Converter.toString(jingShenBSFVo.getZHUYUANQKDM()), valueByCode));
        }
        if (StringUtil.isNullOrEmpty(jingShenBSFVo.getFUYAOYCXMC())) {
            jingShenBSFVo.setFUYAOYCXMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_FYYCX.getCode(), Converter.toString(jingShenBSFVo.getFUYAOYCXDM()), valueByCode));
        }
        if (jingShenBSFVo.getJINGSHENJKFCSDM() != null) {
            jingShenBSFVo.setJINGSHENJKFCSMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV06_00_209.getCode(), Converter.toString(jingShenBSFVo.getJINGSHENJKFCSDM()), valueByCode));
        }
        if (!CollectionUtils.isEmpty(jingShenBSFYYs)) {
            jingShenBSFYYs.forEach(info -> {
                info.setYONGYAOTJMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV06_00_102.getCode(), Converter.toString(info.getYONGYAOTJDM()), valueByCode));
            });
        }
    }

    ZhongXingJSBSF setClinicalDocumentData(ZhongXingJSBSF clinicalDocumentData, JingShenBSFVo jingShenBSFVo, HospitalDTO hospital, BingRenXXDto bingRenInfo) {
        clinicalDocumentData.setEffectiveTime(CDAUtil.toD15(jingShenBSFVo.getLURURQ() != null ? jingShenBSFVo.getLURURQ() : jingShenBSFVo.getSUIFANGRQ()));
        clinicalDocumentData.setPatientRole(createPatientRole(jingShenBSFVo, bingRenInfo));
        clinicalDocumentData.setAuthor(createAuthor(jingShenBSFVo, hospital));
        clinicalDocumentData.setCustodian(createCustodian(hospital));
        return clinicalDocumentData;
    }

    Author createAuthor(JingShenBSFVo jingShenBSFVo, HospitalDTO hospital) {
        Author author = new Author();
        author.setTime(CDAUtil.toD15(jingShenBSFVo.getSUIFANGRQ()));
        author.setId(!StringUtil.isNullOrEmpty(jingShenBSFVo.getSUIFANGYSBM()) ? jingShenBSFVo.getSUIFANGYSBM() : jingShenBSFVo.getSUIFANGYSXM());
        author.setName(jingShenBSFVo.getSUIFANGYSXM());
        author.setOrganizationId(CDAUtil.toJGID10(jingShenBSFVo.getSUIFANGDWBM()));
        author.setOrganizationName(jingShenBSFVo.getSUIFANGDWMC());
        author.setOrganizationTelecom(hospital.getDanWeiDH());
        author.setOrganizationAddr(hospital.getAddress());
        return author;
    }

    Custodian createCustodian(HospitalDTO hospital) {
        Custodian custodian = new Custodian();
        custodian.setId(CDAUtil.toJGID10(hospital.getOutId()));
        custodian.setName(hospital.getName());
        custodian.setTelecom(hospital.getDanWeiDH());
        custodian.setAddr(hospital.getAddress());
        return custodian;
    }

    PatientRole createPatientRole(JingShenBSFVo jingShenBSFVo, BingRenXXDto bingRenInfo) {
        PatientRole patientRole = new PatientRole();
        patientRole.setId(CDAUtil.formatNumber(jingShenBSFVo.getGERENDABS()));
        patientRole.setTelecom(bingRenInfo.getShouJiHao());
        patientRole.setAddr(createAddr(bingRenInfo));
        patientRole.setPatient(createPatient(jingShenBSFVo));
        return patientRole;
    }

    Addr createAddr(BingRenXXDto bingRenXXDto) {
        Addr addr = new Addr();
        addr.setState(bingRenXXDto.getJuZhuDSFMC());
        addr.setCity(bingRenXXDto.getJuZhuDSMC());
        addr.setCounty(bingRenXXDto.getJuZhuDQMC());
        addr.setTownship(bingRenXXDto.getJuZhuDJDMC());
        addr.setStreetName(bingRenXXDto.getJuZhuDSQMC());
        addr.setHouseNumber(bingRenXXDto.getJuZhuDMPH());
        addr.setPostalCode(bingRenXXDto.getLianXiRYB());
        return addr;
    }

    Patient createPatient(JingShenBSFVo jingShenBSFVo) {
        Patient patient = new Patient();
        patient.setId(jingShenBSFVo.getZHENGJIANHM());
        patient.setName(jingShenBSFVo.getXINGMING());
        return patient;
    }

}
